﻿using System;
using System.Linq;
using System.Web.Mvc;
using STX.Service;
using STX.Domain;

using STX.Admin.Models;

namespace STX.Admin.Controllers
{
    public abstract class BasePagesController<T, TM> : AuthController
        where T : BaseEntity
        where TM : BaseQueryModel, new()
    {
        #region 字段
        protected Func<TM, IQueryable<T>, IQueryable<T>> QueryFun;
        #endregion

        #region 构造函数

        protected BasePagesController()
        {
            // ReSharper disable DoNotCallOverridableMethodsInConstructor
            OnInit();
        }
        #endregion

        #region Property

        protected abstract BaseService<T> ThisService { get; }
        #endregion

        #region Method

        #region 抽象方法
        public abstract void OnInit();
        #endregion

        public virtual ActionResult Index()
        {
            return View();
        }

        #region 查询

        public ActionResult Query()
        {
            return Query(new TM
            {
                Page = 1,
                PageSize = 20
            });
        }

        [HttpPost]
        public ActionResult Query(TM query)
        {
            var result = ThisService.FindAll();
            if (QueryFun != null)
            {
                result = QueryFun(query, result);
            }
            var total = result.Count();
            result = result.OrderByDescending(x => x.Id);
            if (query.Page > 1 && query.PageSize > 0)
            {
                result = result.Skip((query.Page - 1) * query.PageSize);
            }
            result = result.Take(query.PageSize);
            var data = new Web.Framework.Query.PagedList<T>()
            {
                Page = query.Page,
                PageSize = query.PageSize,
                Data = result.ToList(),
                Total = total
            };
            return View(data);
        }
        #endregion

        #region 添加
        public virtual ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public virtual JsonResult Save(T model)
        {
            if (model.Id == 0)
            {
                ThisService.Add(model);
            }
            else
            {
                ThisService.Update(model);
            }

            return new JsonResult { Data = new { success = true } };
        }
        #endregion

        #region 删除
        public virtual JsonResult Delete(int[] ids)
        {
            ThisService.RemoveIds(ids);
            return new JsonResult { Data = new { success = true } };
        }
        #endregion

        #region 编辑
        public virtual ActionResult Edit(int id)
        {
            var model = ThisService.FindModelById(id);
            if (model == null)
            {
                throw new Exception("没有找到此条数据！");
            }
            return View(model);
        }
        #endregion

        #endregion

    }
}
